package com.raptor.trustworthycore.listener;

import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;

/**
 * 启动预热
 *
 * @Author renhuan
 * @Date 2021-04-14 17:18
 **/
public class TrustwothyPreparedListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {

  private static final Logger log = LoggerFactory.getLogger(TrustwothyPreparedListener.class);

  @Override
  public void onApplicationEvent(
      ApplicationEnvironmentPreparedEvent applicationEnvironmentPreparedEvent) {

    log.info("启动环境准备，可以在此把一些数据读入缓存");
    //准备属性
    Properties applicationProperties = new Properties();
    applicationProperties.setProperty("spring.jackson.date-format","yyyy-MM-dd HH:mm:ss");
    applicationProperties.setProperty("spring.jackson.time-zone","GMT+8");
    log.info(applicationProperties.toString());
    //携入环境applicationProperties
    ConfigurableEnvironment envi = applicationEnvironmentPreparedEvent.getEnvironment();
    MutablePropertySources mps = envi.getPropertySources();
    if (mps != null) {
      mps.addFirst(new PropertiesPropertySource("application", applicationProperties));
    }
  }
}
